package com.hd.crdao.activity;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hd.crdao.R;
import com.hd.crdao.base.BaseSecondActivity;
import com.hd.crdao.bean.VehCreditProgressBean;
import com.hd.crdao.config.Constants;
import com.hd.crdao.utils.MyToast;
import com.hd.crdao.view.ExpandHeadView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.List;

import butterknife.BindView;
import okhttp3.Call;

/**
 * @author Programming
 * @date 2017/10/18 0018
 */

public class ProgressActivity extends BaseSecondActivity {
    @BindView(R.id.progreassHead)
    ExpandHeadView mProgreassHead;
    @BindView(R.id.progreessRecyclerview)
    RecyclerView mProgreessRecyclerview;
    @BindView(R.id.veBrand)
    TextView mVeBrand;
    @BindView(R.id.veFrame)
    TextView mVeFrame;
    public MyBaseAdpater mMyBaseAdpater;

    @Override
    protected int setContentView() {
        return R.layout.activity_progress;
    }

    @Override
    protected ExpandHeadView setExpandHeadView() {
        return mProgreassHead;
    }

    @Override
    protected void initView() {
        String veBrand = getIntent().getStringExtra("veBrand") + "";
        String veFrame = getIntent().getStringExtra("veFrame") + "";
        mVeBrand.setText(veBrand);
        mVeFrame.setText(veFrame);
        mProgreessRecyclerview.setLayoutManager(new LinearLayoutManager(this));
        initData();
    }

    private void initData() {
        String vehCreditId = getIntent().getStringExtra("vehCreditId") + "";
        OkHttpUtils.post().url(Constants.GETVEHCREDITPROGRESS_URL)
                .addParams("sign", Constants.SIGN)
                .addParams("vehCreditId", vehCreditId).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                MyToast.show("网络开了个小差");
            }

            @Override
            public void onResponse(String response, int id) {
                VehCreditProgressBean vehCreditProgressBean = new Gson().fromJson(response, VehCreditProgressBean.class);
                if (vehCreditProgressBean.getSuccess() == 0) {
                    mMyBaseAdpater = new MyBaseAdpater(ProgressActivity.this, vehCreditProgressBean.getReturnCreditProgressList());
                    mProgreessRecyclerview.setAdapter(mMyBaseAdpater);
                } else {
                    MyToast.show(vehCreditProgressBean.getMessage());
                }
            }
        });
    }

    private class MyBaseAdpater extends RecyclerView.Adapter {
        private LayoutInflater inflater;
        private List<VehCreditProgressBean.ReturnCreditProgressListBean> mProgressListBeen;
        public int mUncheckPosition;


        MyBaseAdpater(Context context, List<VehCreditProgressBean.ReturnCreditProgressListBean> mProgressListBeen) {
            inflater = LayoutInflater.from(context);
            this.mProgressListBeen = mProgressListBeen;
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new ViewHolder(inflater.inflate(R.layout.progress_item, parent, false));
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            ViewHolder itemHolder = (ViewHolder) holder;

            mUncheckPosition = getUncheckPosition();
            if(mProgressListBeen!=null&&mUncheckPosition==0){
                mUncheckPosition=mProgressListBeen.size();
            }
            if (mUncheckPosition == mProgressListBeen.size()) {
                itemHolder.tvBottomLine.setVisibility(View.VISIBLE);
                itemHolder.tvBottomLine.setBackgroundColor(getResources().getColor(R.color.blue_dot));
                itemHolder.tvDot.setBackground(getResources().getDrawable(R.drawable.timelline_dot_first));
                itemHolder.tvTopLine.setBackgroundColor(getResources().getColor(R.color.blue_dot));
                itemHolder.tvProfressRoleName.setTextColor(getResources().getColor(R.color.blue_dot));
                itemHolder.tvProgressName.setTextColor(getResources().getColor(R.color.blue_dot));
                itemHolder.tvprogressDate.setTextColor(getResources().getColor(R.color.blue_dot));
                if (position == 0) {
                    itemHolder.tvTopLine.setVisibility(View.INVISIBLE);
                    itemHolder.tvLeftText.setVisibility(View.VISIBLE);
                } else if (position == mProgressListBeen.size() - 1) {
                    itemHolder.tvBottomLine.setVisibility(View.INVISIBLE);
                    itemHolder.tvLeftText.setVisibility(View.VISIBLE);
                    itemHolder.tvLeftText.setText("结束");
                } else {
                    itemHolder.tvLeftText.setVisibility(View.INVISIBLE);
                }


            } else {
                if (position == 0) {
                    itemHolder.tvTopLine.setVisibility(View.INVISIBLE);
                    itemHolder.tvLeftText.setVisibility(View.VISIBLE);
                    itemHolder.tvBottomLine.setBackgroundColor(getResources().getColor(R.color.blue_dot));
                } else if (position > 0 && position < getUncheckPosition()) {
                    itemHolder.tvTopLine.setVisibility(View.VISIBLE);
                    itemHolder.tvTopLine.setBackgroundColor(getResources().getColor(R.color.blue_dot));
                    itemHolder.tvDot.setBackground(getResources().getDrawable(R.drawable.timelline_dot_first));
                    itemHolder.tvBottomLine.setBackgroundColor(getResources().getColor(R.color.blue_dot));
                    if (position == getUncheckPosition() - 1) {
                        itemHolder.tvBottomLine.setBackgroundColor(getResources().getColor(R.color.gray_dot));
                    }
                } else if (position == mProgressListBeen.size() - 1) {    //最后一个条目
                    itemHolder.tvLeftText.setVisibility(View.VISIBLE);
                    itemHolder.tvLeftText.setText("结束");
                    itemHolder.tvDot.setBackground(getResources().getDrawable(R.drawable.timelline_dot_normal));
                    itemHolder.tvTopLine.setBackgroundColor(getResources().getColor(R.color.gray_dot));
                    itemHolder.tvBottomLine.setVisibility(View.INVISIBLE);
                } else {
                    itemHolder.tvTopLine.setBackgroundColor(getResources().getColor(R.color.gray_dot));
                    itemHolder.tvDot.setBackground(getResources().getDrawable(R.drawable.timelline_dot_normal));
                }

                //给文字染色
                if (position < getUncheckPosition()) {
                    itemHolder.tvProfressRoleName.setTextColor(getResources().getColor(R.color.blue_dot));
                    itemHolder.tvProgressName.setTextColor(getResources().getColor(R.color.blue_dot));
                    itemHolder.tvprogressDate.setTextColor(getResources().getColor(R.color.blue_dot));
                } else {
                    itemHolder.tvProfressRoleName.setTextColor(getResources().getColor(R.color.gray_dot));
                    itemHolder.tvProgressName.setTextColor(getResources().getColor(R.color.gray_dot));
                    itemHolder.tvprogressDate.setTextColor(getResources().getColor(R.color.gray_dot));
                }

            }


            itemHolder.bindData(position);
        }

        @Override
        public int getItemCount() {
            if (mProgressListBeen != null) {
                return mProgressListBeen.size();
            }
            return 0;
        }

        /**
         * 获取未检测的位置
         *
         * @return
         */
        private int getUncheckPosition() {
            if (mProgressListBeen != null) {

                for (int i = 0; i < mProgressListBeen.size(); i++) {
                        if (!mProgressListBeen.get(i).getCompleteFlag()) {
                            return i;
                        }
                }
            }
            return 0;
        }

            class ViewHolder extends RecyclerView.ViewHolder {
                private TextView tvProfressRoleName, tvProgressName, tvprogressDate;
                private TextView tvTopLine, tvDot, tvBottomLine;
                private TextView tvLeftText;


                ViewHolder(View itemView) {
                    super(itemView);
                    //左边
                    tvLeftText = (TextView) itemView.findViewById(R.id.leftText);
                    // 中间
                    tvTopLine = (TextView) itemView.findViewById(R.id.tvTopLine);
                    tvDot = (TextView) itemView.findViewById(R.id.tvDot);
                    tvBottomLine = (TextView) itemView.findViewById(R.id.tvBottmLine);
                    //右边
                    tvProfressRoleName = (TextView) itemView.findViewById(R.id.profressRoleName);       //审核角色
                    tvProgressName = (TextView) itemView.findViewById(R.id.progressName);       //审核进度
                    tvprogressDate = (TextView) itemView.findViewById(R.id.progressDate);       //审核日期


                }

                /**
                 * 为控件设置数据
                 *
                 * @param position
                 */
                private void bindData(int position) {
                    VehCreditProgressBean.ReturnCreditProgressListBean progressListBean = mProgressListBeen.get(position);
                    tvProfressRoleName.setText(progressListBean.getProfressRoleName());
                    tvProgressName.setText(progressListBean.getProgressName());
                    tvprogressDate.setText(progressListBean.getProgressDate());
                }
            }


        }
    }
